package com.wdh.kjs.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wdh.kjs.R;
import com.wdh.kjs.http.Httpurl;

import com.wdh.kjs.model.JsonModel;
import com.wdh.kjs.util.ToastUtils;

/**
 * Created by feifei on 2016/4/21.
 */
public class SecuritySetting extends BaseTopActivity implements View.OnClickListener {
    private LinearLayout security_settint_image;
    private LinearLayout security_settint_image1;
    private LinearLayout security_settint_image2;
    private LinearLayout security_settint_image3;
    private LinearLayout security_settint_image4;
    private LinearLayout security_settint_image5;
    private TextView TextView_realAuth;
    private TextView TextView_tradePwd;
    private Intent intent;
    private String levels;
    private String type;
    private String partId;
    private JsonModel jsonModel;
    private String auditingOpinion="0";
    private String success;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.security_setting);
        SharedPreferences sharedPreferences = getSharedPreferences("logininfo", Context.MODE_PRIVATE);
        levels = sharedPreferences.getString("levels","");
        type = sharedPreferences.getString("type","");
        partId = sharedPreferences.getString("loginToken","");

        initview();
        if(type.equals("p")) {
            initshiming();
        }else if (type.equals("o")){
            initshimingorgnize();
        }
        if (!levels.equals("")) {
            initwidge();
        }
        if (type.equals("o")){
            security_settint_image4.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(type.equals("p")) {
            initshiming();
        }else if (type.equals("o")){
            initshimingorgnize();
        }
        if (!levels.equals("")) {
            initwidge();
        }
        if (type.equals("o")){
            security_settint_image4.setVisibility(View.GONE);
        }
        SharedPreferences.Editor editor=getSharedPreferences("benditupian",MODE_PRIVATE).edit();
        editor.putString("tupian0","");
        editor.putString("tupian1","");
        editor.putString("tupian2","");
        editor.commit();
    }

    private void initshimingorgnize() {
        HttpUtils http = new HttpUtils();
        RequestParams params = new RequestParams();

//            params.addBodyParameter("emailAddress","742051747@qq.com" );
        params.addBodyParameter("partyId", partId);

        http.send(HttpRequest.HttpMethod.POST, Httpurl.URL_HUOQURENZHENG_O, params, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {


                Gson gson = new Gson();
                jsonModel = gson.fromJson(responseInfo.result, JsonModel.class);
                if(jsonModel.success) {

                    auditingOpinion=jsonModel.getRealAuthInfo().getAuditingOpinion();
                    if("0".equals(auditingOpinion)){
                        success="false";
                    }else {
                        success="true";
                    }
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {


            }
        });

    }
    private void initshiming() {
        HttpUtils http = new HttpUtils();
        RequestParams params = new RequestParams();

//            params.addBodyParameter("emailAddress","742051747@qq.com" );
        params.addBodyParameter("partyId", partId);

        http.send(HttpRequest.HttpMethod.POST, Httpurl.URL_HUOQURENZHENG, params, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {


                Gson gson = new Gson();
                jsonModel = gson.fromJson(responseInfo.result, JsonModel.class);
                if(jsonModel.success)
                auditingOpinion=jsonModel.getRealAuthInfo().getAuditingOpinion();
            }

            @Override
            public void onFailure(HttpException error, String msg) {


            }
        });

    }



    private void initwidge() {
        security_settint_image.setOnClickListener(this);
        security_settint_image1.setOnClickListener(this);
        security_settint_image2.setOnClickListener(this);
        security_settint_image3.setOnClickListener(this);
        security_settint_image4.setOnClickListener(this);
        security_settint_image5.setOnClickListener(this);

        if(type.equals("n")){
            TextView_realAuth.setText("去认证");
            TextView_tradePwd.setText("设置");
        }else {
            switch (type){
                case "p":
                    TextView_realAuth.setText("查看");

                    if(levels.equals("1")){
                        TextView_tradePwd.setText("设置");
                    }else{
                        TextView_tradePwd.setText("修改");
                    }
                    break;
                case "o":
                    TextView_realAuth.setText("查看");

                    if(levels.equals("1")||levels.equals("2")){
                        TextView_tradePwd.setText("设置");
                    }else{
                        TextView_tradePwd.setText("修改");
                    }
                    break;
        }
        }
    }

    private void initview() {
        initTopBar("安全设置");
        security_settint_image = (LinearLayout) findViewById(R.id.security_settint_image);
        security_settint_image1 = (LinearLayout) findViewById(R.id.security_settint_image1);
        security_settint_image2 = (LinearLayout) findViewById(R.id.security_settint_image2);
        security_settint_image3 = (LinearLayout) findViewById(R.id.security_settint_image3);
        security_settint_image4 = (LinearLayout) findViewById(R.id.security_settint_image4);
        security_settint_image5 = (LinearLayout) findViewById(R.id.security_settint_image5);
        TextView_realAuth = (TextView)findViewById(R.id.TextView_realAuth);
        TextView_tradePwd = (TextView)findViewById(R.id.TextView_tradePwd);
    }

    @Override
    public void onClick(View v) {
    switch (v.getId()){
    case R.id.security_settint_image:
        if(type.equals("n")){
            intent = new Intent(this, RealNameAuthActivity.class);
            startActivity(intent);
        }else{
            switch (type){
                case "p":
                    if (auditingOpinion.equals("0")) {
//                        intent = new Intent(this, RealNameAuthActivity.class);
                        intent = new Intent(this, RealNameAuthPerActivity.class);
                        intent.putExtra("URL", Httpurl.URL_REALAUTH_PER_BACK);
                    }
                    else
                    intent = new Intent(this, RealNameAuthPerSubActivity.class);
                    startActivity(intent);
                    break;
                case "o":
                    if (!"true".equals(success)) {
                      //  intent = new Intent(this, RealNameAuthActivity.class);
                        intent = new Intent(this, RealNameAuthOrgActivity.class);
                        intent.putExtra("URL", Httpurl.URL_REALAUTH_ORG_BACK);
                    }
                    else
                    intent = new Intent(this, RealNameAuthOrgSubActivity.class);
                    startActivity(intent);
                    break;
            }
        }
        break;
    case R.id.security_settint_image1:
        intent = new Intent(this, BangdingActivity.class);
        startActivity(intent);
        break;
    case R.id.security_settint_image2:
        intent = new Intent(this, UpdateLoginPasswordActivity.class);
        startActivity(intent);
        break;
    case R.id.security_settint_image3:
        if(type.equals("n")){
            ToastUtils.showShort(SecuritySetting.this, "请先进行实名认证！");
        }else{
            switch (type){
                case "p":
                    if(levels.equals("0")||levels.equals("1")){
                        intent = new Intent(this, SetTradePasswordActivity.class);
                        startActivity(intent);
                    }else {
                        intent = new Intent(this, UpdateTradePasswordActivity.class);
                        startActivity(intent);
                    }
                    break;
                case "o":
                    if(levels.equals("0")||levels.equals("1")||levels.equals("2")){
                        intent = new Intent(this, SetTradePasswordActivity.class);
                        startActivity(intent);
                    }else {
                        intent = new Intent(this, UpdateTradePasswordActivity.class);
                        startActivity(intent);
                    }
                    break;
            }
        }
        break;
    case R.id.security_settint_image4:
        if (levels.equals("0")){
            ToastUtils.showShort(SecuritySetting.this, "请先进行实名认证！");
        }else if (levels.equals("1")){
            ToastUtils.showShort(SecuritySetting.this, "请先设置交易密码！");
        }
        else {
            intent = new Intent(this, BankCardActivity.class);
            startActivity(intent);
        }
        break;
    case R.id.security_settint_image5:
        intent = new Intent(this, GesturesPasswordActivity.class);
        startActivity(intent);
        break;
    default:
        break;
    }
    }

}
